package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.service.LeaveStatisticsService;
import cn.sdormitory.report.vo.LeaveStatisticsVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/11/8 23:31
 * @version：V1.0
 */
@RestController
@Api(tags = "Report-LeaveStatistics=> 请假统计")
@RequestMapping("/report/leavestatistics")
public class LeaveStatisticsController {
    @Autowired
    private LeaveStatisticsService leaveStatisticsService;

    @ApiOperation("list => 查询请假统计列表")
    @PreAuthorize("@ss.hasPermi('report:leavestatistics:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<LeaveStatisticsVO>> list(@RequestParam Map<String, Object> params) throws ParseException {
        String yearMonth=(String)params.get("inYearMonth");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        List<LeaveStatisticsVO> leaveStatisticsVOList = leaveStatisticsService.getleaveStaList(inYearMonth,pageNum,pageSize);
        if(leaveStatisticsVOList!=null && leaveStatisticsVOList.size()>0){
            for (LeaveStatisticsVO leaveStatisticsVO:leaveStatisticsVOList){
                leaveStatisticsVO.setInYearMonth(inYearMonth);
            }
        }
        CommonPage<LeaveStatisticsVO> commonPage = new CommonPage<LeaveStatisticsVO>();
        commonPage.setList(leaveStatisticsVOList);
        Long count=leaveStatisticsService.getleaveStaListCount(inYearMonth);
        commonPage.setTotal(count);
        return CommonResult.success(commonPage);

    }

    @ApiOperation("leavelist =>导出宿舍请假统计信息")
    @PreAuthorize("@ss.hasPermi('report:leavestatistics:leavelist')")
    @GetMapping(value = "/exportleavelist")
    public CommonResult export(HttpServletResponse response, @RequestParam Map<String, Object> params) throws ParseException {
        String yearMonth=(String)params.get("inYearMonth");
        String inYearMonth="";
        if("".equals(yearMonth) || yearMonth==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            inYearMonth=date1;
        }else{
            inYearMonth=yearMonth;
        }

        List<LeaveStatisticsVO> leaveStatisticsVOList =  leaveStatisticsService.getleaveStaListExport(inYearMonth);
        try {
            EasyExcel.write(response.getOutputStream(), LeaveStatisticsVO.class).sheet("宿舍请假统计管理").doWrite(leaveStatisticsVOList);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
